import $ from "jquery";
import {config, url} from "../../util/config";
import {message} from "antd";
import login from "../login";
function getNewLogin(item) {
    return getResourceLib(item)
}
function getListByArea(ARR,num) {
    let list = ARR.map(item=>{
        if(item.LAYOUT_AREA==num){
            item.FILE_ARR.map(item=>{
                item.key = item.FILE_ID
                // if(count==1){
                //     if(item.FILE_TYPE == "folder"){
                //         item.FILE_TYPE = config.folder
                //     }
                //     else if(item.FILE_TYPE == "article"){
                //         item.FILE_TYPE = config.article
                //     }
                //     else if(item.FILE_TYPE == "file"){
                //         item.FILE_TYPE = config.file
                //     }
                // }
                console.log(`LAYOUT_AREA==${num}:`,item)
            })
            return {
                moduleContent:item.MODULE_CONTENT,
                layoutArea:item.LAYOUT_AREA,
                key:item.MODULE_ID,
                moduleName:item.MODULE_NAME,
                moduleId:item.MODULE_ID,
                fileArr:item.FILE_ARR,
                moduleType:item.MODULE_TYPE
            }
        }
    });
    return list.filter(item=>item!=undefined || item!=null)
}
export default function getResourceLib(item) {

    console.log("type:folder-d , id:",item)
    let token = localStorage.getItem("token")
    let list1 = [];
    let list2 = [];
    let list3 = [];
    let list4 = [];
    let layoutType = null;
    let count = null;
    $.ajax({
        async:false,
        method:"GET",
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        url: url.api_url+`UserPage/GetFileList?folder_d=${item}`,
        success: function (result) {
            if(result.result=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                let tmp = getNewLogin(item)
                count = tmp.count
                layoutType = tmp.layoutType
                list1 = tmp.list1
                list2 = tmp.list2
                list3 = tmp.list3
                list4 = tmp.list4
            }else {
                count = result.result.COUNT;
                layoutType = result.result.LAYOUT_TYPE
                if(layoutType==1){
                    list1 = getListByArea(result.result.ARR,"1");
                }else if(layoutType==2){
                    list1 = getListByArea(result.result.ARR,"1");
                    list2 = getListByArea(result.result.ARR,"2");
                }else if(layoutType==3){
                    list1 = getListByArea(result.result.ARR,"1");
                    list2 = getListByArea(result.result.ARR,"2");
                    list3 = getListByArea(result.result.ARR,"3");
                }else if(layoutType=="4"){
                    list1 = getListByArea(result.result.ARR,"1");
                    list2 = getListByArea(result.result.ARR,"2");
                    list3 = getListByArea(result.result.ARR,"3");
                    list4 = getListByArea(result.result.ARR,"4");
                }
            }
        },
        error:function (error) {
            message.error("请求异常");
        }
    })
    return {
        count,
        layoutType,
        list1,
        list2,
        list3,
        list4
    }
}
